package com.ermax.aircond.maintain.web.bean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ermax.aircond.maintain.domain.Building;
import com.ermax.aircond.maintain.domain.Floor;
import com.ermax.aircond.maintain.service.MaintainService;
import com.ermax.aircond.resource.MessageBundle;
import com.ermax.aircond.util.web.bean.DataTableBean;

@Scope("view")
@Component
public class FloorMaintainBean extends DataTableBean implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(FloorMaintainBean.class);
	@Autowired
	private MaintainService maintainService;	
	@ManagedProperty(value = "#{loginBean.sessionUser}")
	private String sessionUser;

	private Floor editedItem;
	private List<Floor> floors = new ArrayList<Floor>();
	private List<SelectItem> buildingOptions = new ArrayList<SelectItem>();
	private long selectedBuildingId;

	@PostConstruct
	public void init() {
		try {			
			reset();
		} catch (Exception ex) {
			String message = MessageBundle.getInstance().getMessage("error_init_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale());
			FacesMessage facesMessage = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		}
	}

	private void reset() throws Exception {
		floors = maintainService.getAllFloors();
		buildingOptions = createBuildingOptions();
		editedItem = new Floor();		
	}

	private List<SelectItem> createBuildingOptions() {
		List<SelectItem> results = new ArrayList<SelectItem>();		
		try {
			List<Building> buildings = maintainService.getAllBuildings();
			for (Building building : buildings) {
				SelectItem item = new SelectItem(building.getBuildingId(), building.getBuildingName());
				results.add(item);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		return results;
	}

	public void create() {
		try {
			Building building = maintainService.getBuildingById(selectedBuildingId);
			editedItem.setBuilding(building);
			maintainService.createFloor(editedItem, sessionUser);
			reset();
		} catch (Exception ex) {
			String message = MessageBundle.getInstance().getMessage("error_create_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale());
			FacesMessage facesMessage = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		}
	}

	public void update() {
		try {
			Building building = maintainService.getBuildingById(selectedBuildingId);
			editedItem.setBuilding(building);
			maintainService.updateFloor(editedItem, sessionUser);
			reset();
		} catch (Exception ex) {
			String message = MessageBundle.getInstance().getMessage("error_update_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale());
			FacesMessage facesMessage = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		}
	}

	public void remove() {
		try {
			Floor floor = floors.get(getCurrentIndex());
			maintainService.deleteFloor(floor);
			reset();
		} catch (Exception ex) {
			String message = MessageBundle.getInstance().getMessage("error_remove_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale());
			FacesMessage facesMessage = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		}
	}
	
	public void search() {
		try{
			Building building = null;
			if(selectedBuildingId > 0){
				building = maintainService.getBuildingById(selectedBuildingId);
			}

			floors = maintainService.getFloors(building, getSearchStr());
		}catch(Exception ex){}
	}	

	public MaintainService getMaintainService() {
		return maintainService;
	}

	public void setMaintainService(MaintainService maintainService) {
		this.maintainService = maintainService;
	}

	public Floor getEditedItem() {
		return editedItem;
	}

	public void setEditedItem(Floor editedItem) {
		this.editedItem = editedItem;
	}

	public List<Floor> getFloors() {
		return floors;
	}

	public void setFloors(List<Floor> floors) {
		this.floors = floors;
	}

	public long getSelectedBuildingId() {
		return selectedBuildingId;
	}

	public void setSelectedBuildingId(long selectedBuildingId) {
		this.selectedBuildingId = selectedBuildingId;
	}

	public List<SelectItem> getBuildingOptions() {
		return buildingOptions;
	}

	public void setBuildingOptions(List<SelectItem> buildingOptions) {
		this.buildingOptions = buildingOptions;
	}

}
